# 使用 NVIDIA CUDA 基础镜像
FROM nvidia/cuda:12.1.0-cudnn8-devel-ubuntu22.04
# nvidia/cuda:12.4.1-cudnn-devel-ubuntu22.04
# 设置环境变量
ENV TZ=Asia/Shanghai \
    DEBIAN_FRONTEND=noninteractive

RUN sed -i 's@archive.ubuntu.com@mirrors.aliyun.com@g' /etc/apt/sources.list \
    && sed -i 's@security.ubuntu.com@mirrors.aliyun.com@g' /etc/apt/sources.list

# 更新软件包列表并安装必要工具
RUN DEBIAN_FRONTEND=noninteractive apt-get update \
    && apt-get install -y \
        python3.10 \
        python3-pip \
        git \
        net-tools \
        tzdata \
        aria2 \
        vim \
        curl \
        wget \
        lsof \
        gcc \
        python3.10-dev \
        build-essential \
        openssh-server \
    && rm -rf /var/lib/apt/lists/* \
    && apt clean

# 设置时区
RUN ln -fs /usr/share/zoneinfo/$TZ /etc/localtime \
    && echo $TZ > /etc/timezone


# 安装 SSH server 并配置 root 登录
RUN mkdir -p /var/run/sshd /root/.ssh && \
    echo 'root:rootroot' | chpasswd && \
    sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
    sed -i 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config


# 创建符号链接以方便使用 python, pip 等命令
RUN rm -f /usr/bin/python /usr/bin/pip && \
    ln -s /usr/bin/python3.10 /usr/bin/python && \
    ln -s /usr/bin/pip3 /usr/bin/pip

WORKDIR /app
COPY requirements.txt /app


# 设置 pip 源
RUN echo "[global]" > /etc/pip.conf && \
    echo "index-url = https://mirrors.aliyun.com/pypi/simple" >> /etc/pip.conf && \
    echo "trusted-host = mirrors.aliyun.com" >> /etc/pip.conf

RUN mkdir -p ~/.config/uv && \
    echo "[[index]]\nurl = 'https://mirrors.aliyun.com/pypi/simple'\ndefault = true" >> ~/.config/uv/uv.toml

# 升级 pip 并安装 PyTorch 及其他依赖
RUN pip install --no-cache-dir --upgrade pip \
    && pip install uv \
    && uv pip install --no-cache-dir -r requirements.txt --system

# 暴露 SSH 端口和可能使用的其他端口
EXPOSE 22

# 暴露端口, 供外界调用
EXPOSE 13145

# 如果生产上部署如果是这样的话，就不能远程了
# CMD ["gunicorn", "--workers", "4", "--bind", "0.0.0.0:10086", "app:app"]
# bash gunicorn --workers 4 --bind 0.0.0.0:10086 app:app
# 设置容器启动命令
CMD ["/usr/sbin/sshd", "-D"]